package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.StateData;
import com.woniu.dao.mapper.YClassTypeMapper;
import com.woniu.model.model.YClassType;
import com.woniu.model.model.YGoods;
import com.woniu.model.param.ClassTypeAddParam;
import com.woniu.model.param.PageClassTypeParam;
import com.woniu.model.param.PageParam;
import com.woniu.service.YClassTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class YClassTypeServiceImpl extends ServiceImpl<YClassTypeMapper, YClassType> implements YClassTypeService {

    @Resource
    private YClassTypeMapper classTypeMapper;


    /*
    新增课程类别的方法
     */
    @Override
    public YClassType addBookType(ClassTypeAddParam param) throws YogaException {
        if (param.getTypeName() == null) {
            throw new YogaException(500, "输入为空，请重新输入");
        }
        QueryWrapper<YClassType> wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_name", param.getTypeName());
        wrapper.ne("class_type_state",StateData.CLASS_TYPE_STATE_DELETED);
        YClassType yClassType = classTypeMapper.selectOne(wrapper);
        // 只有在类别名存在且状态不是已删除时才抛出异常
        if (yClassType != null && yClassType.getClassTypeState()!=StateData.CLASS_TYPE_STATE_DELETED) {
            throw new YogaException(500, "课程类别已重复，请重新输入");
        }
        YClassType type = new YClassType();
        type.setClassTypeName(param.getTypeName());
        type.setClassTypeState(StateData.CLASS_TYPE_STATE_DISABLED);
        classTypeMapper.insert(type);

        return type;
    }

    /*
    禁用课程类别
     */
    @Override
    public void disable(Integer classTypeId) throws YogaException {
        YClassType type = classTypeMapper.selectById(classTypeId);
        if (type == null) {
            throw new YogaException(500, "不存在该类别，禁用失败");
        }
        if (Objects.equals(type.getClassTypeState(), StateData.CLASS_TYPE_STATE_DISABLED)) {
            throw new YogaException(500, "该类别状态已是禁用");
        }
        UpdateWrapper<YClassType> wrapper = new UpdateWrapper<>();
        wrapper.set("class_type_state", StateData.CLASS_TYPE_STATE_DISABLED);
        wrapper.eq("class_type_id", classTypeId);
        classTypeMapper.update(type, wrapper);
    }

    /*
    启用课程类别
     */
    @Override
    public void enable(Integer classTypeId) throws YogaException {
        YClassType type = classTypeMapper.selectById(classTypeId);
        if (type == null) throw new YogaException(500, "不存在该类别");
        if (Objects.equals(type.getClassTypeState(), StateData.CLASS_TYPE_STATE_START))
            throw new YogaException(500, "类别状态异常");
        UpdateWrapper<YClassType> wrapper = new UpdateWrapper<>();
        wrapper.set("class_type_state", StateData.CLASS_TYPE_STATE_START);
        wrapper.eq("class_type_id", classTypeId);
        classTypeMapper.update(null, wrapper);
    }

    /*
    分页查询
     */
    @Override
    public Page<YClassType> loadPageClassType(PageClassTypeParam param) throws YogaException {
        Page<YClassType> page = new Page<>(param.getPageNum(), param.getPageSize());

        QueryWrapper<YClassType> wrapper = new QueryWrapper<>();
        wrapper.like("class_type_name",param.getTypeName());

        wrapper.ne("class_type_state", StateData.CLASS_TYPE_STATE_DELETED);
        return classTypeMapper.selectPage(page, wrapper);
    }

    /**
     * 查询所有课程类型
     * @return
     */
    @Override
    public List<YClassType> loadClassTypeList() {
        List<YClassType> list = classTypeMapper.selectList(new QueryWrapper<YClassType>().eq("class_type_state", StateData.CLASS_TYPE_STATE_START));
        if (list.size() == 0){
            throw new YogaException(3100,"课程类型不存在");
        }
        return list;
    }

    /**
     * 删除课程类别
     *
     * @param classTypeId：类别id
     * @return
     * @throws YogaException
     */
    @Override
    public void deleteType(Integer classTypeId) throws YogaException {
        YClassType classType = baseMapper.selectById(classTypeId);
        if (classType==null){
            throw new YogaException(500,"删除失败，还没有该类别");
        }
        if (classType.getClassTypeState()==StateData.CLASS_TYPE_STATE_DELETED){
            throw new YogaException(500,"删除失败，已是删除状态");
        }
        if (classType.getClassTypeState()==StateData.CLASS_TYPE_STATE_START){
            throw new YogaException(500,"正在使用中，删除失败");
        }

        classType.setClassTypeState(StateData.CLASS_TYPE_STATE_DELETED);
        baseMapper.updateById(classType);
    }


}
